package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import cn.wolfcode.crm.util.UploadUtil;
import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @RequestMapping("")
    public String index(){
        return "employee/list";
    }
    @RequestMapping("person")
    public String person(){
        return "employee/person2";
    }
    @Autowired
    private ServletContext ctx;


    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("employee:data")
    @PermissionName("员工数据")
    public PageResult list(EmployeeQueryObject qo){
        return service.query(qo);
    }

    @RequestMapping("getCurrentUser")
    @ResponseBody
    public Employee getCurrentUser(){
            Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
            //把当前用户信息发送回去
        return currentUser;
    }
    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("employee:save")
    @PermissionName("员工保存")
    public JsonResult save(Employee employee){
        JsonResult result = new JsonResult();
        try {
            service.save(employee);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("employee:update")
    @PermissionName("员工更新")
    public JsonResult update(Employee employee){
        JsonResult result = new JsonResult();
        try {
            service.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("编辑失败");
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("employee:changeState")
    @PermissionName("员工状态")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            service.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

    @RequestMapping("queryEmployee")
    @ResponseBody
    public List<Employee> queryEmployee(){

        return   service.queryEmployee();
    }

    @RequestMapping("editPerson")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee e,MultipartFile pic){
        //调用工具类中的文件上传方法执行上传操作
        //获取到文件在项目中的路径
        if(pic!=null){
            if(!StringUtils.isEmpty(e.getImg())){
                UploadUtil.deleteFile(ctx,e.getImg());
            }
            String realPath = ctx.getRealPath("/upload");
            String imagePath = UploadUtil.upload(pic,realPath);
            e.setImg(imagePath);
        }
        JsonResult result = new JsonResult();
        try {
            if(e.getId()!=null){
                service.update(e);
            }else{
                service.save(e);
            }
        } catch (Exception er) {
            er.printStackTrace();
            result.setMsg("操作失败");
        }
        return result;
    }

}
